import { bg_image } from "./indoorConfig.js";
import {getMerchantInfo} from "../api/home/index.js"
import {rechargeActivityList,merchantRecharge} from "../api/merchant/index.js"
export default {
	data() {
		return {
			bg_image,
			userinfo: {},
			amountList: [],
			selectItem: {},
			payInfo:{},
			data:{}
		}
	},
	onReady() {
		this.fetchSomething()
	},
	onPullDownRefresh(){//页面下拉刷新
		this.fetchSomething()
		uni.stopPullDownRefresh();
	},
	methods: {
		async fetchSomething(query){
			this.getRechargeActivityList()
			this.getDetail()
		},
		async getDetail(){
			let res = await getMerchantInfo()
			this.data = res.data || {}
			this.userinfo = res.data || {}
		},
		async getRechargeActivityList() {
			let res = await rechargeActivityList()
			let data = res.data || []
			this.amountList = data
			this.selectItem = data[0] || {}
		},
		handleSelect(item) {
			this.selectItem = item
		},
		async pay() {
			if(!this.selectItem.id) return this.$u.toast('请选择充值套餐');
			let params = {
				"rechargeId": this.selectItem.id,
				"realMoney": this.selectItem.realMoney,
				"gainMoney": this.selectItem.gainMoney,
				"merchantId": this.userinfo.id
			}
			let res = await merchantRecharge(params)
			this.payInfo = res.data || {}
			this.onBridgeReady()
		},
		async onBridgeReady() {
			let Provider =  await uni.getProvider({service: 'payment'})
			let payCanshu = {provider: Provider.provider[0] || 'wxpay',}
			payCanshu = {
				...payCanshu,
				orderInfo:this.payInfo.tradeNo,
			}
			let rePayment = await uni.requestPayment({...payCanshu});
			let flags = rePayment.errMsg == 'requestPayment:ok' && rePayment.resultCode != '6001'
			if(flags) {
				uni.navigateBack()
			}else{
				this.$u.toast('支付失败');
			}
		}
	}
}